package com.tanhua.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.interceptor.UserHolder;
import com.tanhua.model.dimain.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.UserInfoServer;
import io.jsonwebtoken.Claims;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author 杰哥不要啦~
 * @date 2021/10/27
 */
@RestController
@RequestMapping("/users")

public class UsersContoller {

    @Autowired
    private UserInfoServer userInfoServer;
    /**
     * 查询用户信息
     *1.请求头token
     * 2.请求参数userid
     */
    @GetMapping
    public ResponseEntity users(@RequestHeader("Authorization") String token,Long userID){


        //3.判断id是否为空
        if (userID == null){
            userID = Long.valueOf(UserHolder.getUserid());
        }
        UserInfoVo userInfo = userInfoServer.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo,@RequestHeader("Authorization") String token){


        //3.设置用户id
        userInfo.setId(UserHolder.getUserid());
        //4.更新
        userInfoServer.update(userInfo);
        return ResponseEntity.ok(null);
    }
}
